/*
 * Copyright (c) 2006, 2022, webrx.cn All rights reserved.
 *
 */
package cn;
import java.io.FileInputStream;
import java.util.Date;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.LocalDateTime;

import cn.webrx.config.SpringConfig;
import cn.webrx.entity.*;
import cn.webrx.mapper.BookMapper;
import cn.webrx.mapper.DbMapper;
import cn.webrx.mapper.GoodsMapper;
import cn.webrx.mapper.StudentMapper;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>Project: mybatis2022 - Demo
 * <p>Powered by webrx On 2022-04-11 14:08:06
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
@SpringJUnitConfig(classes = SpringConfig.class)
public class Demo {

    @Autowired(required = false)
    GoodsMapper gm;

    @Autowired(required = false)
    IdWorker idw;

    @Autowired(required = false)
    SnowflakeIdWorker sfidw;

    //@Autowired(required = false)
    //private IdWorker idw;

    @Test
    public void id1(){
        System.out.println(sfidw.nextId());
        System.out.println(sfidw.nextId());
        System.out.println(sfidw.nextId());
        System.out.println(sfidw.nextId());
        System.out.println("--------------------------");
        System.out.println(idw.nextId());
        System.out.println(idw.nextId());
        System.out.println(idw.nextId());
    }

    @Test
    void hello(){
        System.out.printf("hello world%n");
    }

    @Test
    void g1(){
        Goods goods = new Goods();
        goods.setId(0L);
        goods.setName("计算机键盘");
        goods.setPrice(98.2);
        goods.setLocalTime(LocalTime.now());
        goods.setLocalDate(LocalDate.now());
        goods.setCreateTime(LocalDateTime.now());
        goods.setUpdateTime(new Date());
        goods.setOnline(true);

        //读取图片文件字节流，
        try(FileInputStream fis = new FileInputStream("d:/key.jpg")){
            goods.setGoodsPic(fis.readAllBytes());
        }catch(Exception e){

        }

        goods.setGoodsDescription("这是一个超级键盘.....");


        int i = gm.save(goods);
        if(i>0){
            System.out.println("商品添加成功");
        }
    }



    @Autowired(required = false)
    StudentMapper sm;

    @Test
    void s4(){
        StduentVo st = new StduentVo();
        st.setSid(2022300);
        st.setName("王一");
        System.out.println(st.getSid());
        int i = sm.insert(st);
        if(i>0){
            System.out.println("数据插入成功");
            System.out.println(st);
            System.out.println(st.getSid());
        }
    }

    @Test
    void s3(){
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("name","Jack111");
        System.out.println(map.get("sid"));

        int i = sm.insert(map);
        if(i>0){
            System.out.println("数据插入成功");
            System.out.println(map);
            System.out.println(map.get("sid"));
        }
    }

    @Test
    void s2(){
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("name","Jack2");
        System.out.println(map.get("id"));

        int i = sm.save(map);
        if(i>0){
            System.out.println("数据插入成功");
            System.out.println(map);
            System.out.println(map.get("id"));
        }
    }

    @Test
    void s1(){
        Student stu = new Student();
        stu.setName("张三丰");
        System.out.println(stu.getId());

        int i = sm.add(stu);
        if(i>0){
            System.out.println("数据插入成功");
            System.out.println(stu);
            System.out.println(stu.getId());
        }
    }




    @Autowired(required = false)
    BookMapper bm;

    @Autowired(required = false)
    DbMapper dm;

    @Test
    void t10(){
        System.out.println(dm.dbs());
        System.out.println(dm.tbss("mysql"));
        System.out.println(dm.tbs());
        dm.createDb("mybatisdb2022");
        System.out.println(dm.count("oadb.book"));
    }


    @Test @DisplayName("测试查询")
    void t9(){
        System.out.println(bm.maxPrice());
        System.out.println(bm.count());
        System.out.println(bm.selectList());
        System.out.println(bm.selectListMap());
    }


    @Test
    void t8(){
        select();
        int i = bm.updates(-3);
        System.out.println(i);
        if(i>0){
            System.out.println("数据修改成功");
        }
        select();

    }


    @Test
    void t7(){
        Map<String,Object> map = new HashMap<>();
        map.put("id",1);
        map.put("pr",66);
        map.put("author","王五");
        int i = bm.update(map);
        if(i>0){
            System.out.println("数据修改成功");
        }

    }

    @Test
    void t6(){
        Book t = bm.queryById(14);
        t.setAuthor("JACK");

        int i = bm.update(t);
        if(i>0){
            System.out.println("数据修改成功"+t);
        }

    }

    @Test
    void t5(){
        System.out.println(bm.queryById(7));
    }

    @Test @DisplayName("bookmapper 删除数据")
    void delete(){
        //int i = bm.deleteById(17);

        //int i = bm.deleteByName("《前端开发》");
        //System.out.println(i);

        //int i = bm.deleteByIds(new int[]{3,4,9});
        //System.out.println(i);

        Map<String,Object> map = new HashMap<>();
        map.put("id",6);
        map.clear();
        map.put("ids",new int[]{13,11,15});

        int i = bm.deleteByIdss(map);


        if(i>0) System.out.println("删除成功");
        select();
    }


    @Test @DisplayName("bookmapper 插入数据")
    void insert(){
        //int i = bm.save("《mybatis入门与精通》","李丽",55.8);

        //Map<String,Object> map = new HashMap<String, Object>();
        //map.put("nn","《样式编程》");
        //map.put("author","李强");
        //map.put("price",99);

        //int i = bm.add(map);

        //Map<String,Object> map = new HashMap<>();
        //map.put("name","《javascript程序设计入门2》");
        //map.put("price",28);
        //int i = bm.add(map);
        //System.out.println(map);

        int i = bm.save("《my001》","jack",22); //拦截器会自动增加一个 id =

        Book bk = new Book();
        bk.setName("《java开发》");
        bk.setAuthor("李四");
        bk.setPrice(66);

        //int i = bm.bookSave(bk);
        if(i>0){
            System.out.println("数据插入成功,id = "+bk.getId());
        }
        System.out.println("-----------------------------");
        select();
    }

    @Test @DisplayName("bookmapper 查询数据")
    void select(){
        for (Map<String, Object> m : bm.query()) {
            System.out.println(m.get("id"));
            System.out.println(m.get("name"));
        }
    }
}
